from msgpasser import *;
from msg import *;
import sys;

#http://www.python.org/doc/essays/graphs.html
def find_shortest_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not graph.has_key(start):
        return None
    shortest = None
    for node in graph[start]:
        if node not in path:
            newpath = find_shortest_path(graph, node, end, path)
            if newpath:
                if not shortest or len(newpath) < len(shortest):
                    shortest = newpath
    return shortest

def nextHop(G, start, end):
    shortestPath = find_shortest_path(G, start, end)
    if len(shortestPath) > 1:
        return shortestPath[1]
    else:
        return "BAD"


def getForwardingTable(yamlUrl, source):
    myConfigFile = configfile(yamlUrl)
    configuration = myConfigFile.getConfiguration()
    graph = {};
    table = {};
    for data in configuration:
        graph[data['name']] = data['group']
    for data in configuration:
        if(data['name'] != source):
            table[data['name']] = nextHop(graph, source, data['name'])
    print table
    return table

def get_node_neighbors(yamlUrl, node):
    myConfigFile = configfile(yamlUrl)
    configuration = myConfigFile.getConfiguration()
    graph = {};
    table = {};
    for data in configuration:
        if(data['name'] == node):
            return data['group']
    return 0

getForwardingTable('demo.yaml', 'a')
